//
//  BoxMangerViewController.m
//  cMeeting
//
//  Created by 梦居 on 2019/1/10.
//  Copyright © 2019 中移（杭州）信息技术有限公司. All rights reserved.
//

#import "BoxMangerViewController.h"
#import "BoxMangerCell/BoxMangerTableViewCell.h"
#import "BandingViewController.h"
#import "BoxFamilyMangerViewController.h"
#import "QRCodeViewController.h"
@interface BoxMangerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)NSArray * dataArray;
@property (nonatomic,strong)UITableView * tableV;
@end

@implementation BoxMangerViewController
- (void)getData{
//    [BBRSHNet uploadLookToken:@"776" block:^(id posts, NSInteger code, NSString *Meg) {
//        NSLog(@"%@",posts);
//    }];
    NSDictionary * dic = @{@"family_id":@"776"};
    
    [MDNetDetail GETNet:MYBox_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
        NSLog(@"%@",posts);
        self.dataArray = posts[@"data"];
      
        [self.tableV reloadData];
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"机顶盒管理";
     self.tableV = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, MDSCREEN_WIDTH, MDSCREEN_HEIGHT - 100) style:(UITableViewStylePlain)];

    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    [self.view addSubview:self.tableV];
    [self.tableV registerClass:[BoxMangerTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    UIButton * newBoxBtn = [[UIButton alloc]initWithFrame:CGRectMake(100, MDSCREEN_HEIGHT - 80, MDSCREEN_WIDTH - 200, 50)];
    newBoxBtn.backgroundColor = [UIColor colorWithHexStr:@"#6fac45"];
    [newBoxBtn setTitle:@"绑定新的机顶盒" forState:(UIControlStateNormal)];
    newBoxBtn.layer.cornerRadius = 25;
    newBoxBtn.layer.masksToBounds = true;
    [newBoxBtn addTarget:self action:@selector(addNewClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:newBoxBtn];
    [self getData];
}
- (void)addNewClick{
    BandingViewController * bandingVC = [[BandingViewController alloc]init];
    [self.navigationController pushViewController:bandingVC animated:true];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return 170;
//}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    BoxMangerTableViewCell * cell = [[BoxMangerTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
    if (self.dataArray.count > 0 ) {
        NSDictionary * dic  = self.dataArray[indexPath.row];
        cell.nameLabel.text = dic[@"box_name"];
        NSArray * array = dic[@"family_member"];
        [cell sendArray:array];
    }
    cell.backgroundColor = [UIColor whiteColor];
    self.tableV.rowHeight = cell.heightCell;
    
    cell.rebandingBlock = ^(NSString * _Nonnull str) {
        NSDictionary * dicBox  = self.dataArray[indexPath.row];
        QRCodeViewController * qrcodeVC = [[QRCodeViewController alloc]init];
        
        qrcodeVC.block = ^(NSString * _Nonnull str) {
            [MBProgressHUD showActivityMessageInView:@""];
            NSDictionary * dic = @{@"box_id":dicBox[@"box_id"],@"new_box_id":str};
            [MDNetDetail GETNet:MemberRebanding_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
                [MBProgressHUD hideHUD];
                [MBProgressHUD showInfoMessage:Meg];
                if (code == 0) {
                    [self getData];
                }
                
                
            }];
        };
        [self.navigationController pushViewController:qrcodeVC animated:true];
    };
    cell.deleteBlock = ^(NSString * _Nonnull str) {
        UIAlertController * alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定删除吗？" preferredStyle:(UIAlertControllerStyleAlert)];
        UIAlertAction * actionSure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            NSDictionary * iddic  = self.dataArray[indexPath.row];
            NSDictionary * dic = @{@"box_id":iddic[@"box_id"]};
            [MDNetDetail POSTNet:DeleteBox_url param:dic block:^(id posts, NSInteger code, NSString *Meg) {
                if (code == 0) {
                    [MBProgressHUD showInfoMessage:@"删除成功"];
                    [self getData];
                }
                else
                {
                    [MBProgressHUD showInfoMessage:Meg];
                }
            }];
        }];
        UIAlertAction * cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:cancel];
        [alertC addAction:actionSure];
        [self presentViewController:alertC animated:true completion:nil];
    };
    cell.menberBlock = ^(NSString * _Nonnull str) {
        
        BoxFamilyMangerViewController * family = [[BoxFamilyMangerViewController alloc]init];
        NSDictionary * iddic  = self.dataArray[indexPath.row];
        family.box_id = iddic[@"box_id"];
        NSDictionary * dic = self.dataArray[indexPath.row];
        NSArray * array = dic[@"family_member"];
        
        family.array = array;
        [self.navigationController pushViewController:family animated:true];
    };
    return cell;
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    [self getData];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
